package com.kintreda.ieco_server.core.service.impl;

import java.util.List;
import java.util.Map;

import com.kintreda.ieco_server.core.service.RedisService;
import com.kintreda.ieco_server.util.Assert;
import com.kintreda.ieco_server.util.DateUtil;
import com.kintreda.ieco_server.util.redis.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * redis缓存管理
 * @author HuangZhongYao
 *
 */
@Service("redisService")
public class RedisServiceImpl implements RedisService {

	@SuppressWarnings("rawtypes")
	@Autowired
	public RedisTemplate redisTemplate;
	
	public static final String TOKEN_SATE = "AC47337522AB1C26BC2ECA0B52E14EC3";
	
	/***************************************************************
	 * 登录成功后 按用登录手机号获得token
	 * @param userId
	 * @return
	 */
	public String tokenKey(String userId) {
		if(Assert.hasText(userId)) {
			return RedisKey.tokenKey(userId);
		} else {
			return "";
		}
	}
	
	/***************************************************************
	 * @param key
	 * @param object
	 */
	@SuppressWarnings("unchecked")
	public void setString(String key,String object,int outSeconds) {
		this.redisTemplate.opsForValue().set(key, object);
		expireAt(key, outSeconds);
	}
	
	
	/***************************************************************
	 * 
	 * @param key
	 * @param request
	 * @return
	 */
	public String getString(String key) {
		
		Object object = this.redisTemplate.opsForValue().get(key);
		if(object != null) {
			return object.toString();
		} else {
			return null;
		}
	}
	
	
	/***************************************************************
	 * 删除key
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public void remove(String key) {
		
		this.redisTemplate.delete(key);
	}
	
	
	/***************************************************************
	 * 设置redis key 对应的过期时间
	 * @param key
	 * @param outSeconds	分钟值
	 */
	@SuppressWarnings("unchecked")
	public void expireAt(String key,int outSeconds) {
		
		this.redisTemplate.expireAt(key, DateUtil.getCurrentBeforeDateByMinute(outSeconds));
	}
	
	/***************************************************************
	 * 向key对应的map中添加缓存对象
	 * @param key
	 * @param map
	 */
	@SuppressWarnings("unchecked")
	public void addMap(String key,Map<String, Object> map) {
		
		this.redisTemplate.opsForHash().putAll(key, map);
	}
	
	
	/***************************************************************
	 * @param key
	 * @param map
	 */
	@SuppressWarnings("unchecked")
	public void setMap(String key,Map<String, Object> map,int outSeconds) {
		
		this.redisTemplate.opsForHash().putAll(key, map);
		expireAt(key, outSeconds);
	}
	
	
	/***************************************************************
	 * 
	 * @param key
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> getMap(String key) {
		
		return this.redisTemplate.opsForHash().entries(key);
	}
	
	
	/***************************************************************
	 * 存储list对象
	 * @param key
	 * @param list
	 */
	@SuppressWarnings("unchecked")
	public void setList(String key,List<Object> list,int outSeconds) {
		
		redisTemplate.opsForList().rightPushAll(key, list);
		expireAt(key, outSeconds);
	}
	
	
	/***************************************************************
	 * 获取list对象
	 * @param key
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Object> getList(String key) {
		
		return redisTemplate.opsForList().range(key, 0, 1);
	}
}
